<!doctype html>
<html ng-app>
	<head>
		<script src="angular.js"></script>
		<script src="geolib.js"></script>
		<script src="geophil.js"></script>		
		<script src="gz_2010_us_050_00_500k.js"></script>		
		<script src="benchmark.js"></script>
	</head>
	<body>
	
		<div ng-controller="resultCtrl">
			<form ng-submit="addresult()">
				<h4>Faire Init puis ensuite Search</h4>
				<div><ul><li>angular.js n'est pas necessaire, elle sert a l'affichage</li>
					<li>geolib.js n'est pas necessaire, elle sert aux calculs geographique</li>
					<li>departement.js n'est pas necessaire, ce sont les departement francais au format geojson</li>					
					<li>geophil.js est la librairie qui constitue les bandes de recherche</li>
					<li>Search example:  l=18.1 L=-66.9 Yauco is last in array. l=35.1 L=-84.8 Bradley is the last of biggest band</li>
				</ul></div>
				<div>
					<label>Band width</label>
					<input type="number" ng-model="BandWidth" placeholder="Enter a band width here">
					<span>BW= {{BandWidth}}</span>
				</div>
				<div>
					<label>Latitude:</label>
					<input type="number" ng-model="yourLatitude" placeholder="Enter a latitude here">
					<span>Lat= {{yourLatitude}}</span>
				</div>
				<div>
					<label>Longitude:</label>
					<input type="number" ng-model="yourLongitude" placeholder="Enter a longitude here">
					<span>Long= {{yourLongitude}}</span>
				</div>
				<input class="btn-primary" type="button" ng-click="doInit()" value="Init">
				<input class="btn-primary" type="button" ng-click="doTestSpeed()" value="Test Speed">
				<input class="btn-primary" type="button" ng-click="doTestDiff()" value="Test Diff">
				<input class="btn-primary" type="button" ng-click="doSearchBench()" value="Benchmark Search">				
				<input class="btn-primary" type="submit" value="Search">
			</form>
			<hr>

			<table>
				<thead>
					<tr>
						<th>Latitude</th>
						<th>Longitude</th>
						<th>Departement</th>						
					</tr>
				</thead>
				<tbody>
					<tr ng-repeat="a_result in ResultList">
						<td ng-repeat="toto in a_result track by $index">{{toto}}</td>
					</tr>
				</tbody>
			</table>			
		</div>

	<script>
		function resultCtrl($scope) {			
			$scope.doInit = function() {
				$scope.ResultList = [];
				$scope.sliceZone = new ClassSliceZone(uscounties, 30);
				if ($scope.BandWidth > 0)
					$scope.sliceZone.prepare_zone($scope.BandWidth);
				else $scope.sliceZone.prepare_zone();
				$scope.ResultList.push(["Min: " + $scope.sliceZone.MMCZ.minLat, "Min: " + $scope.sliceZone.MMCZ.minLng, ""]);
				$scope.ResultList.push(["Max: " + $scope.sliceZone.MMCZ.maxLat, "Max: " + $scope.sliceZone.MMCZ.maxLng, ""]);
				$scope.ResultList.push(["Med: " + $scope.sliceZone.MMCZ.medLat, "", ""]);
				res = $scope.sliceZone.stats_Band();
				for (z in res) {
					$scope.ResultList.push(res[z]);
				}
			};

			$scope.doTestSpeed = function() {
				$scope.sliceZone.defaultTestingValues(33,41,-120,-78);
				var start = new Date().getTime();
				$scope.sliceZone.testingSpeedIdx();
				var end = new Date().getTime();
				var timeSearchIdx = end - start;				
				$scope.ResultList.push(["Indexed search", "Time", timeSearchIdx]);				
				$scope.ResultList.push(["Nb Test Success ", ":", $scope.sliceZone.speedTest.NbTestSuc]);
				$scope.ResultList.push(["Nb Test Fail ", ":",  $scope.sliceZone.speedTest.NbTestFail]);
				
				$scope.sliceZone.defaultTestingValues(33,41,-120,-78);
				var start = new Date().getTime();
				$scope.sliceZone.testingSpeedSeq();
				var end = new Date().getTime();
				var timeSearchSeq = end - start;				
				$scope.ResultList.push(["Sequential search", "Time", timeSearchSeq]);
				$scope.ResultList.push(["Nb Test Success ", ":", $scope.sliceZone.speedTest.NbTestSuc]);
				$scope.ResultList.push(["Nb Test Fail ", ":",  $scope.sliceZone.speedTest.NbTestFail]);
				$scope.ResultList.push(["Ratio", ":", Math.floor(timeSearchSeq/timeSearchIdx)]);
			
				
				/*var suite = new Benchmark.Suite;
				suite.add('Indexed', function() {
					$scope.sliceZone.testingSpeedIdx();
				}).add('Sequential', function() {
					$scope.sliceZone.testingSpeedSeq();
				}).on('cycle', function(event) {
					$scope.ResultList.push(["cycle", ":", String(event.target)]);
				}).on('complete', function() {
					$scope.ResultList.push(["Fastest is", ":", this.filter('fastest').pluck('name')]);
				}).run();*/
			};
			
			
			$scope.doTestDiff = function() {
				$scope.sliceZone.defaultTestingValues(33,41,-120,-78);				
				res = $scope.sliceZone.testingDiff();
				for (z in res){
					$scope.ResultList.push(res[z]);
				}				
			};

			$scope.doSearchBench = function() {
				var suite = new Benchmark.Suite;
				suite.add('Indexed', function() {

					$scope.sliceZone.search_pointinside({latitude: $scope.yourLatitude, longitude: $scope.yourLongitude});

				}).add('Sequential', function() {

					$scope.sliceZone.search_pointinside_gj({latitude: $scope.yourLatitude, longitude: $scope.yourLongitude});

				}).on('cycle', function(event) {
					$scope.ResultList.push(["cycle", ":", String(event.target)]);
				}).on('complete', function() {
					$scope.ResultList.push(["Fastest is", ":", this.filter('fastest').pluck('name')]);
				}).run();
			};
			
			$scope.addresult = function() {							
				/* Recherche dans la structure preparee indexee */
				console.time("timing IDX");
				var start = new Date().getTime();
				if ($scope.sliceZone.search_pointinside({latitude: $scope.yourLatitude, longitude: $scope.yourLongitude})) {
					$scope.ResultList.push([$scope.yourLatitude, $scope.yourLongitude, $scope.sliceZone.foundfeature.properties.NAME]);
				}
				else {
					$scope.ResultList.push([$scope.yourLatitude, $scope.yourLongitude, "Not found"]);
				}
				var end = new Date().getTime();
				var timeSearch = end - start;				
				$scope.ResultList.push(["Indexed search", "Time", timeSearch]);				
				console.timeEnd("timing IDX");
				
				/* Recherche brute sequentielle */
				console.time("timing SEQ");
				var start = new Date().getTime();				
				if ($scope.sliceZone.search_pointinside_gj({latitude: $scope.yourLatitude, longitude: $scope.yourLongitude})) {
					$scope.ResultList.push([$scope.yourLatitude, $scope.yourLongitude, $scope.sliceZone.foundfeature.properties.NAME]);
				}
				else {
					$scope.ResultList.push([$scope.yourLatitude, $scope.yourLongitude, "Not found"]);
				}				
				var end = new Date().getTime();
				var timeSearch = end - start;				
				$scope.ResultList.push(["Sequential search", "Time", timeSearch]);
				console.timeEnd("timing SEQ");
			};
		}		
	</script>	
	</body>
</html>
